<template>
  <h1>老人信息</h1>
  <div>
    <el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="100px">
      <el-form-item label="老人姓名" prop="elderName">
        <el-input v-model="formData.elderName" type="text" placeholder="请输入老人姓名" clearable
          :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item label="身份号" prop="idCardNo">
        <el-input v-model="formData.idCardNo" type="text" placeholder="请输入身份号" clearable
          :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker v-model="formData.birthday" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
          :style="{ width: '100%' }" placeholder="请选择出生日期" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="formData.age" type="text" placeholder="请输入年龄" clearable :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="genger">
        <el-radio-group v-model="formData.genger" size="default">
          <el-radio v-for="(item, index) in gengerOptions" :key="index" :label="item.value"
            :disabled="item.disabled">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="头像" prop="file" required>
        <el-upload ref="file" :file-list="filefileList" :action="fileAction" :before-upload="fileBeforeUpload">
          <el-button size="small" type="primary" icon="el-icon-upload">选择头像</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import {
  ElMessage
}
  from 'element-plus'
const formRef = ref()
const data = reactive({
  formData: {
    elderName: undefined,
    idCardNo: undefined,
    birthday: null,
    age: undefined,
    genger: 0,
    file: null,
  },
  rules: {
    elderName: [{
      required: true,
      message: '请输入老人姓名',
      trigger: 'blur'
    }],
    idCardNo: [{
      required: true,
      message: '请输入身份号',
      trigger: 'blur'
    }],
    birthday: [{
      required: true,
      message: '请选择出生日期',
      trigger: 'change'
    }],
    age: [{
      required: true,
      message: '请输入年龄',
      trigger: 'blur'
    }],
    genger: [{
      required: true,
      message: '性别不能为空',
      trigger: 'change'
    }],
  }
})
const {
  formData,
  rules
} = toRefs(data)
const gengerOptions = ref([{
  "label": "男",
  "value": 1
}, {
  "label": "女",
  "value": 2
}])
// 上传请求路径
const fileAction = ref('https://jsonplaceholder.typicode.com/posts/')
// 上传文件列表
const filefileList = ref([])
/**
 * @name: 上传之前的文件判断
 * @description: 上传之前的文件判断，判断文件大小文件类型等
 * @param {*} file
 * @return {*}
 */
function fileBeforeUpload(file) {
  let isRightSize = file.size / 1024 / 1024 < 2
  if (!isRightSize) {
    ElMessage.error('文件大小超过 2MB')
  }
  return isRightSize
}
/**
 * @name: 表单提交
 * @description: 表单提交方法
 * @return {*}
 */
function submitForm() {
  formRef.value.validate((valid) => {
    if (!valid) return
    // TODO 提交表单
  })
}
/**
 * @name: 表单重置
 * @description: 表单重置方法
 * @return {*}
 */
function resetForm() {
  formRef.value.resetFields()
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}
</style>
